﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using xiushen.Domain;
using xiushen.Utilies;

namespace xiushen.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        private IAdminService service = new AdminRepository();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login(string userName, string password)
        {
         
            if (service.ValiDateUser(userName, password))
            {
                FormsAuthentication.SetAuthCookie(userName,
                      createPersistentCookie: true);
                return RedirectToAction("Index", "Admin");
            }
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(Users data)
        {
            data.Salt = Guid.NewGuid().ToString();
            data.PasswordHash = Encryptor.MD5Hash(data.PasswordHash, data.Salt);
            var createData = service.PersistData(data);

            return Login(createData.Name, createData.PasswordHash);
        }
    }
}
  